package br.uff.es2.xadrez.core;

public class King extends Piece {

    public King(Color color, String imageFilePath) {
        this.color = color;
        this.imageFilePath = imageFilePath;
        this.moved = false;
    }

    @Override
    public int[][] getAvaiableMoves(int[] posicao, Piece[][] board) {
        int[][] availableMoves = new int[8][8];
        for (int x = posicao[0] - 1; x < posicao[0] + 2; x++) {
            if (x >= 0 && x < 8) {
                for (int y = posicao[1] - 1; y < posicao[1] + 2; y++) {
                    if (y >= 0 && y < 8) {
                        if (board[x][y] == null) {
                            availableMoves[x][y] = 1;
                        } else {
                            if (board[x][y].getColor() != getColor()) {
                                availableMoves[x][y] = 1;
                            }
                        }
                    }
                }
            }
        }
        if (getColor() == color.WHITE) {
            if (board[7][7] != null) {
                if (!isMoved() && board[posicao[0]][posicao[1] + 1] == null && board[posicao[0]][posicao[1] + 2] == null && (!board[7][7].isMoved())) {
                    availableMoves[7][6] = 3;
                    Board.roque.setCor(color.WHITE);
                }
            }
            if (board[7][0] != null) {
                if (!isMoved() && board[7][3] == null && board[7][2] == null && board[7][1] == null && !board[7][0].isMoved()) {
                    availableMoves[7][2] = 3;
                    Board.roque.setCor(Color.WHITE);

                }
            }
        } else {
            if (board[0][7] != null) {
                if (!isMoved() && board[0][5] == null && board[0][6] == null && !board[0][7].isMoved()) {
                    availableMoves[0][6] = 3;
                    Board.roque.setCor(Color.BLACK);
                }
            }
            if(board[0][0] != null){
                if (!isMoved() && board[0][3] == null && board[0][2] == null && board[0][1] == null && !board[0][0].isMoved()) {
                    availableMoves[0][2] = 3;
                    Board.roque.setCor(Color.BLACK);
                }
            }

        }
        return availableMoves;
    }
}
